package com.newcoder.controller.advice;


import com.newcoder.util.CustomException;
import com.newcoder.util.ResponseWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice(annotations = Controller.class)
public class WebExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(WebExceptionAdvice.class);
    /**
     * 全局异常处理类
     */

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseWrapper customerException(CustomException e) {                   //客户端异常(自定义异常)
        return ResponseWrapper.exceptionError(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseWrapper serveException(Exception e) {                            //服务器端异常
        return ResponseWrapper.exceptionError(500, e.getMessage());
    }

}
